home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / Thread Manager / Sample Applications / 68k Examples / Single Intersection Threads / UEventMgmt.p < prev    next >
Encoding:
Text File  |  1994-11-17  |  24.6 KB  |  716 lines  |  [TEXT/MPS ]

  1. {–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  2.  
  3.     PROJECT:        Threads Traffic Simulation
  4.     
  5.     FILE:            UEventMgmt.p
  6.     
  7.     LANGUAGE:        MPW Pascal (version 3.2)
  8.         
  9.     DESCRIPTION:    This is the main clearing house for Event Handling.  Once the application
  10.                     is initialized program control is transfered here.  All Event dispatching
  11.                     then takes place from here.
  12.         
  13.     AUTHOR(S):        William H. Knott
  14.                     Apple Computer
  15.                     Cupertino, CA  95014
  16.                     AppleLink : KNOTT
  17.     
  18.     Change History:
  19.                     3/8/93        ewa        Update to new gestalt names
  20.                     
  21.     VERSION(S):        1.0        14-Aug-92    WHK    Brand New Today.
  22.  
  23. –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––}
  24.  
  25. UNIT UEventMgmt;
  26.  
  27. INTERFACE
  28.  
  29. USES
  30.     MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, Traps, GestaltEqu,
  31.     
  32.     Threads, 
  33.     
  34.     UApplication, UInitMgmt, USimulation, UDocument;
  35.  
  36. PROCEDURE Segment_UEventMgmt;
  37.  
  38. PROCEDURE RunApplication;
  39. PROCEDURE CleanUpOnExit;
  40.  
  41. PROCEDURE SetMenuBarState;
  42.  
  43. IMPLEMENTATION
  44.  
  45. {$S EventMgmt}
  46. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  47. {                                                                                      }
  48. {    Segment_UEventMgmt                                                                  }
  49. {                                                                                      }
  50. {    Provided as a convenient way of unloading the UEventMgmt segment when needed      }
  51. {                                                                                      }
  52. {    October 13, 1991        Created by William Knott                                  }
  53. {                                                                                      }
  54. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  55. PROCEDURE Segment_UEventMgmt;
  56.     BEGIN
  57.     END;
  58.  
  59. {$S EventMgmt}
  60. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  61. {                                                                                      }
  62. {    SetMenuItemState                                                                  }
  63. {                                                                                      }
  64. {    Sets the given menu item to either an enable or disabled state.                      }
  65. {                                                                                      }
  66. {    October 13, 1991    WHK        Created today                                          }
  67. {                                                                                      }
  68. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  69. PROCEDURE SetMenuItemState(theMenu    : MenuHandle;
  70.                             theItem    : INTEGER;
  71.                             state    : BOOLEAN);
  72.     BEGIN
  73.         IF state THEN
  74.             EnableItem(theMenu, theItem)
  75.         ELSE
  76.             DisableItem(theMenu, theItem)
  77.     END;
  78.     
  79. {$S EventMgmt}
  80. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  81. {                                                                                      }
  82. {    SetMenuBarState                                                                      }
  83. {                                                                                      }
  84. {    Set the states of the menus as approprate.                                          }
  85. {                                                                                      }
  86. {    October 13, 1991    WHK        Created today                                          }
  87. {                                                                                      }
  88. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  89. PROCEDURE SetMenuBarState;
  90.     VAR
  91.         theMenu            : MenuHandle;
  92.         meFrontWind        : BOOLEAN;
  93.         docWindFront    : BOOLEAN;
  94.         theWind            : WindowPtr;
  95.         daInFront        : BOOLEAN;
  96.     BEGIN
  97.         theWind := FrontWindow;
  98.         meFrontWind := (theWind  <> NIL) & ((WindowPeek(theWind)^.windowKind = 8) | (WindowPeek(theWind)^.windowKind = 2));
  99.         docWindFront := meFrontWind & IsWindowADocument(theWind);
  100.         daInFront := (FrontWindow <> NIL) & NOT meFrontWind;
  101.         
  102.         theMenu := GetMenu(rFileMenuID);        
  103.         SetMenuItemState(theMenu, iFileQuitItem, TRUE);        { Only enable quit, it is the only one we are using right now!    }    
  104.  
  105.         SetMenuItemState(theMenu, 0 , (gDocument <> NIL));            
  106.         DrawMenuBar;
  107.     END;
  108.     
  109. {$S EventMgmt}
  110. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  111. {                                                                                      }
  112. {    YieldFilter                                                                          }
  113. {                                                                                      }
  114. {    Generic routine to handle a dialog box requiring only an OK button to be hit.      }
  115. {    The OK button needs to be the first item in the dialog's DITL.  If this routine      }
  116. {    is being called, an alert should probably be used instead.                           }
  117. {                                                                                      }
  118. {    July 14, 1991        WHK        Created today                                          }
  119. {                                                                                      }
  120. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  121. FUNCTION YieldFilter (theDialog    : DialogPtr;
  122.                 VAR theEvent    : EventRecord;
  123.                 VAR itemHit        : INTEGER)    : BOOLEAN;
  124.     CONST
  125.         kEnterKey        = 3;
  126.         kReturnKey        = 13;
  127.     VAR
  128.         error         : OSErr;
  129.         holdPort    : GrafPtr;
  130.         aItem        : Handle;
  131.         aItemType    : INTEGER;
  132.         aRect        : RECT;
  133.         key            : Char;
  134.         oldTick        : LONGINT;
  135.     BEGIN
  136.         GetPort(holdPort);
  137.         error := YieldToAnyThread;
  138.         SetPort(holdPort);
  139.         YieldFilter := FALSE;
  140.         
  141.         IF theEvent.what = updateEvt THEN
  142.             BEGIN
  143.                 GetDItem(theDialog, 1, aItemType, aItem, aRect);
  144.                 PenSize(3,3);
  145.                 InsetRect(aRect, -4, -4);
  146.                 FrameRoundRect(aRect, 16, 16);
  147.                 PenSize(1,1);
  148.             END;
  149.             
  150.         IF theEvent.what = keyDown THEN
  151.             BEGIN
  152.                 key := CHR(BAnd(theEvent.message, charCodeMask));
  153.                 IF Ord(key) IN [kEnterKey, kReturnKey] THEN
  154.                     BEGIN
  155.                         itemHit := 1;
  156.                         oldTick := TickCount;
  157.                         GetDItem(theDialog, 1, aItemType, aItem, aRect);
  158.                         HiliteControl(ControlHandle(aItem),1);
  159.                         repeat
  160.                         until TickCount > (oldTick + 3);
  161.                         HiliteControl(ControlHandle(aItem),0);
  162.                         YieldFilter := TRUE;
  163.                     END;
  164.             END;
  165.         
  166.     END;
  167.  
  168. {$S EventMgmt}
  169. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  170. {                                                                                      }
  171. {    DoOKDialogImplementation                                                          }
  172. {                                                                                      }
  173. {    Generic routine to handle a dialog box requiring only an OK button to be hit.      }
  174. {    The OK button needs to be the first item in the dialog's DITL.  If this routine      }
  175. {    is being called, an alert should probably be used instead.                          }
  176. {                                                                                      }
  177. {    October 13, 1991    WHK        Created today                                          }
  178. {                                                                                      }
  179. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  180. PROCEDURE DoOKDialogImplementation(dialogID    : INTEGER);
  181.     VAR
  182.         theDialog    : DialogPtr;
  183.         itemHit        : INTEGER;
  184.         oldPort        : GrafPtr;
  185.         error        : OSErr;
  186.     BEGIN
  187.         GetPort(oldPort);
  188.         theDialog := GetNewDialog(dialogID,NIL, Pointer(-1));
  189.         IF theDialog <> NIL THEN
  190.             BEGIN
  191.                 SetPort(theDialog);
  192.                 ShowWindow(theDialog);
  193.                 repeat
  194.                     ModalDialog(@YieldFilter,itemHit);
  195.                 until itemHit = 1;
  196.                 DisposDialog(theDialog);
  197.             END
  198.         ELSE
  199.             Sysbeep(1);
  200.         SetPort(oldPort);
  201.     END;
  202.     
  203. {$S EventMgmt}
  204. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  205. {                                                                                      }
  206. {    DoNotYetImplemented                                                                  }
  207. {                                                                                      }
  208. {    Display the dialog when a non-implemented function is chosen                      }
  209. {                                                                                      }
  210. {    October 13, 1991    WHK        Created today                                          }
  211. {                                                                                      }
  212. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  213. PROCEDURE DoNotYetImplemented;
  214.     BEGIN
  215.         DoOKDialogImplementation(rNotYetImplementedID);
  216.     END;
  217.     
  218. {$S EventMgmt}
  219. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  220. {                                                                                      }
  221. {    DoAboutBox                                                                          }
  222. {                                                                                      }
  223. {    Display the about box, currently a boring static dialog.                           }
  224. {                                                                                      }
  225. {    October 13, 1991    WHK        Created today                                          }
  226. {                                                                                      }
  227. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  228. PROCEDURE DoAboutBox;
  229.     BEGIN
  230.         DoOKDialogImplementation(rAboutDlogID);
  231.     END;
  232.  
  233. {$S EventMgmt}
  234. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  235. {                                                                                      }
  236. {    DoDeskAcc                                                                          }
  237. {                                                                                      }
  238. {    Given the desk accessory that was chosen from the apple menu, this procedure will }
  239. {    launch the selected desk accessory.                                                  }
  240. {                                                                                      }
  241. {    October 13, 1991    WHK        Created today                                          }
  242. {                                                                                      }
  243. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  244. PROCEDURE DoDeskAcc( item : INTEGER );
  245. VAR
  246.     savePort          : GrafPtr;
  247.     refNum            : Integer;
  248.     dName             : Str255;
  249.     theMenu    : MenuHandle;
  250. BEGIN
  251.     GetPort(savePort);
  252.     theMenu := GetMenu(rAppleMenuID);
  253.     GetItem(theMenu, Item, dName);
  254.     refNum := OpenDeskAcc(dName);
  255.     SetPort(savePort);
  256. END;
  257.  
  258. {$S EventMgmt}
  259. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  260. {                                                                                      }
  261. {    DoAppleMenu                                                                          }
  262. {                                                                                      }
  263. {    If a menu event occurs in the apple menu, there are two distinct cases that we      }
  264. {    need to handle.  The first case being the about item, in which we simply display  }
  265. {    the about box. If the item chosen was not the about item we must open the          }
  266. {    selected desk accessory.                                                          }
  267. {                                                                                      }
  268. {    October 13, 1991    WHK        Created today                                          }
  269. {                                                                                      }
  270. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  271. PROCEDURE DoAppleMenu(menuItem : INTEGER);
  272.     BEGIN
  273.         IF menuItem = iAboutItem THEN
  274.             DoAboutBox
  275.         ELSE
  276.             BEGIN
  277.                 DoDeskAcc(menuItem);
  278.             END;
  279.     END;
  280.     
  281. {$S EventMgmt}
  282. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  283. {                                                                                      }
  284. {    DoWindowClose                                                                      }
  285. {                                                                                      }
  286. {    Close the given window.  Currently this document only has a single window,so very }
  287. {    little redirection is being done.                                                  }
  288. {                                                                                      }
  289. {    October 13, 1991    WHK        Created today                                          }
  290. {                                                                                      }
  291. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  292. PROCEDURE DoWindowClose(whichWind    : WindowPtr);
  293.     BEGIN
  294.         IF whichWind <> NIL THEN
  295.             BEGIN
  296.                 IF  IsWindowADocument(whichWind) THEN
  297.                     CloseDocument(whichWind);
  298.             END;
  299.     END;
  300.  
  301. {$S EventMgmt}
  302. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  303. {                                                                                      }
  304. {    DoQuitApplication                                                                  }
  305. {                                                                                      }
  306. {    Sets the global variable gDone to TRUE.  It then closes any open documents.          }
  307. {    This quit can be cancelled by the close document routine.                            }
  308. {                                                                                      }
  309. {    October 13, 1991    WHK        Created today                                          }
  310. {    July 20, 1991        WHK        Added BeginThreadCritical to solve some problems like }
  311. {                                drawing to the screen.                                  }
  312. {                                                                                      }
  313. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  314. PROCEDURE DoQuitApplication;
  315.     VAR
  316.         error    : OSErr;
  317.     BEGIN
  318.         error := ThreadBeginCritical;    { We still have threads spinning away and I am about ready to snatch the        }
  319.                                         { document window out from underneath them, not a very nice thing to do, but    }
  320.                                         { should eliminate some of those unexpected crashes.                            }
  321.         gDone := TRUE;
  322.         CloseAllOpenDocuments;
  323.     END;
  324.  
  325. {$S EventMgmt}
  326. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  327. {                                                                                      }
  328. {    DoFileMenu                                                                          }
  329. {                                                                                      }
  330. {    The menu item that has been selected was somewhere in the File menu.  Therefore   }
  331. {    program flow is directed to the proper routine.                                      }
  332. {                                                                                      }
  333. {    October 13, 1991    WHK        Created today                                          }
  334. {                                                                                      }
  335. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  336. PROCEDURE DoFileMenu(menuItem : INTEGER);
  337.     BEGIN
  338.         CASE menuItem OF
  339.             iFileNewItem:        
  340.                 BEGIN
  341.                     CreateNewDocument;
  342.                     DebugStr('Should not be selectable');
  343.                 END;
  344.             iFileOpenItem:        DoNotYetImplemented;
  345.             iFileCloseItem:        DoWindowClose(FrontWindow);
  346.             iFileSaveItem:        DoNotYetImplemented;
  347.             iFileSaveAsItem:    DoNotYetImplemented;
  348.             iFileRevertItem:        DoNotYetImplemented;
  349.             iFilePSetupItem:    DoPageSetupDoc(FrontWindow);
  350.             iFilePrintItem:        DoPrintDocument(FrontWindow);
  351.             iFileQuitItem:        DoQuitApplication;
  352.         END;
  353.     END;
  354.  
  355. {$S EventMgmt}
  356. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  357. {                                                                                      }
  358. {    DoEditMenu                                                                          }
  359. {                                                                                      }
  360. {    The menu item that has been selected was somewhere in the Edit menu.  Therefore   }
  361. {    program flow is directed to the proper routine.  The Edit Menu is not yet          }
  362. {    implemented                                                                       }
  363. {                                                                                      }
  364. {    October 13, 1991    WHK        Created today                                          }
  365. {                                                                                      }
  366. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  367. PROCEDURE DoEditMenu(menuItem : INTEGER);
  368.     BEGIN
  369.         CASE menuItem OF
  370.             iEditUndoItem:        DoNotYetImplemented;
  371.             iEditCutItem:        DoNotYetImplemented;
  372.             iEditCopyItem:        DoNotYetImplemented;
  373.             iEditPasteItem:        DoNotYetImplemented;
  374.             iEditClearItem:        DoNotYetImplemented;
  375.         END;
  376.     END;
  377.  
  378. {$S EventMgmt}
  379. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  380. {                                                                                      }
  381. {    DoTestMenu                                                                          }
  382. {                                                                                      }
  383. {    The menu item that has been selected was somewhere in the Test menu.  Therefore   }
  384. {    program flow is directed to the proper routine.                                   }
  385. {                                                                                      }
  386. {    October 13, 1991    WHK        Created today                                          }
  387. {                                                                                      }
  388. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  389. PROCEDURE DoTestMenu(menuItem : INTEGER);
  390.     BEGIN
  391.         IF menuItem = 1 THEN
  392.             CreateFreeThreadsForCars
  393.         ELSE
  394.             MarkACarForDestruction;
  395.     END;
  396.  
  397. {$S EventMgmt}
  398. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  399. {                                                                                      }
  400. {    DoMenuCommand                                                                      }
  401. {                                                                                      }
  402. {    The last event was a menu command, either a mouse driven one or a command key        }
  403. {    selection.  From here, it is sent on to individual menu handlers.                  }
  404. {                                                                                      }
  405. {    October 13, 1991    WHK        Created today                                          }
  406. {                                                                                      }
  407. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  408. PROCEDURE DoMenuCommand(whichmenu    : LONGINT);
  409.     VAR
  410.         menuID        : INTEGER;
  411.         menuItem    : INTEGER;
  412.     BEGIN
  413.         menuItem := LoWord(whichmenu);
  414.         menuID := HiWord(whichmenu);
  415.         CASE menuID OF
  416.             rAppleMenuID:    DoAppleMenu(menuItem);
  417.             rFileMenuID:    DoFileMenu(menuItem);
  418.             rEditMenuID:    DoEditMenu(menuItem);
  419.             rTestMenuID:    DoTestMenu(menuItem);
  420.             OTHERWISE    ;
  421.         END;
  422.         HiliteMenu(0);
  423.     END;
  424.     
  425. {$S EventMgmt}
  426. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  427. {                                                                                      }
  428. {    GrowTheWindow                                                                      }
  429. {                                                                                      }
  430. {    Window growing is a unique case that should be handled individually by each          }
  431. {    window kind that exists in an application.  For this reason, the GrowTheWindow      }
  432. {    routine passes the grow event onto the proper handler.                              }
  433. {                                                                                      }
  434. {    October 13, 1991    WHK        Created today                                          }
  435. {    October 17, 1991    WHK        Updated handling of document event so that they are      }
  436. {                                all routed through a single procedure.                  }
  437. {                                                                                      }
  438. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  439. PROCEDURE GrowTheWindow(theWindow    : WindowPtr;
  440.                                 theEvent    : EventRecord);
  441.     BEGIN
  442.         IF IsWindowADocument(theWindow) THEN
  443.             HandleDocumentEvent(theWindow, theEvent);
  444.     END;
  445.  
  446. {$S EventMgmt}
  447. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  448. {                                                                                      }
  449. {    DoMouseDownInContent                                                              }
  450. {                                                                                      }
  451. {    A mousedown occured within the content region of a window.  Since the window may  }
  452. {    react its own unique way depending on whether it is frontmost or not, I will pass }
  453. {    the event onto the window that it occured in.                                      }
  454. {                                                                                      }
  455. {    October 13, 1991    WHK        Created today                                          }
  456. {    October 17, 1991    WHK        Updated handling of document event so that they are      }
  457. {                                all routed through a single procedure.                  }
  458. {                                                                                      }
  459. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  460. PROCEDURE DoMouseDownInContent(theWindow    : WindowPtr;
  461.                                     theEvent    : EventRecord);
  462.     BEGIN
  463.         IF IsWindowADocument(theWindow) THEN
  464.             HandleDocumentEvent(theWindow, theEvent);
  465.     END;
  466.  
  467. {$S EventMgmt}
  468. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  469. {                                                                                      }
  470. {    DoMouseDown                                                                          }
  471. {                                                                                      }
  472. {    Most window mousedowns can be handled generically regardless of the window they   }
  473. {    occur in, the exceptions being the content, grow, zoomIn, and zoomOut regions.    }
  474. {    So all the generic areas are handled here, with special cases being sorted        }
  475. {    elsewhere.                                                                          }
  476. {                                                                                      }
  477. {    October 13, 1991    WHK        Created today                                          }
  478. {    October 17, 1991    WHK        Modified to support mdown in content area Added          }
  479. {                                proper support for mouse down in window not in front  }
  480. {                                                                                      }
  481. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  482. PROCEDURE DoMouseDown(theEvent    : EventRecord);
  483.     VAR
  484.         theWind    : WindowPtr;
  485.         where        : INTEGER;
  486.         bounds        : RECT;
  487.     BEGIN
  488.         where := FindWindow(theEvent.where,theWind);
  489.         
  490.         IF (where IN [inDrag,inGoAway,inGrow,inContent,inZoomIn,inZoomOut]) THEN
  491.             IF (theWind <> NIL) & (theWind <> frontWindow) THEN 
  492.                 SelectWindow(theWind);
  493.         CASE where OF
  494.             inMenuBar:    DoMenuCommand(MenuSelect(theEvent.where));
  495.             inDrag:        DragWindow(theWind,theEvent.where,screenbits.bounds);
  496.             inGoAway:    IF TrackGoAway(theWind,theEvent.where) THEN DoWindowClose(theWind);
  497.             inGrow:        GrowTheWindow(theWind,theEvent);
  498.             inContent:    DoMouseDownInContent(theWind, theEvent);
  499.             (*
  500.             inSysWindow,inZoomIn,inZoomOut
  501.             *)
  502.             OTHERWISE
  503.                 ;
  504.         END;
  505.     END;
  506.  
  507. {$S EventMgmt}
  508. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  509. {                                                                                      }
  510. {    DoKeyDown                                                                          }
  511. {                                                                                      }
  512. {    A keydown has occured, we need to determine whether the command key was down as      }
  513. {    well.  If it is a command key combination, we pass control onto the menu command  }
  514. {    handler, else it is passed to the document window.                                  }
  515. {                                                                                      }
  516. {    October 13, 1991    WHK        Created today                                          }
  517. {    October 17, 1991    WHK        Updated handling of document event so that they are      }
  518. {                                all routed through a single procedure.                  }
  519. {                                                                                      }
  520. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  521. PROCEDURE DoKeyDown(theEvent    : EventRecord);
  522.     VAR
  523.         key        : Char;
  524.     BEGIN
  525.         key := CHR(BAnd(theEvent.message, charCodeMask));
  526.         IF BAnd(theEvent.modifiers, cmdKey) <> 0 THEN
  527.             DoMenuCommand(MenuKey(key))
  528.         ELSE
  529.             IF IsWindowADocument(FrontWindow) THEN
  530.                 HandleDocumentEvent(FrontWindow, theEvent);
  531.     END;
  532.  
  533. {$S EventMgmt}
  534. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  535. {                                                                                      }
  536. {    DoUpdate                                                                          }
  537. {                                                                                      }
  538. {    A update event has occured, so pass the event on to the approprate window.          }
  539. {                                                                                      }
  540. {    October 13, 1991    WHK        Created today                                          }
  541. {    October 17, 1991    WHK        Updated handling of document event so that they are      }
  542. {                                all routed through a single procedure.                  }
  543. {                                                                                      }
  544. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  545. PROCEDURE DoUpdate(theEvent    : EventRecord);
  546.     VAR
  547.         theWind    : WindowPtr;
  548.         holdPort    : GrafPtr;
  549.     BEGIN
  550.         theWind := WindowPtr(theEvent.message);
  551.         
  552.         IF IsWindowADocument(theWind) THEN
  553.             HandleDocumentEvent(theWind, theEvent);
  554.     END;
  555.  
  556. {$S EventMgmt}
  557. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  558. {                                                                                      }
  559. {    DoActivate                                                                          }
  560. {                                                                                      }
  561. {    Whenever a window is brought to the front or moved from the front behind other    }
  562. {    windows an activate event occurs.  This is where controls should be highlighted/  }
  563. {    dehighlighted and menu items should be enabled/disabled approprately.              }
  564. {                                                                                      }
  565. {    October 13, 1991    WHK        Created today                                          }
  566. {    October 17, 1991    WHK        Updated handling of document event so that they are      }
  567. {                                all routed through a single procedure.                  }
  568. {                                                                                      }
  569. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  570. PROCEDURE DoActivate(theEvent    : EventRecord);
  571.     VAR
  572.         theWind    : WindowPtr;
  573.     BEGIN
  574.         theWind := WindowPtr(theEvent.message);
  575.         
  576.         IF IsWindowADocument(theWind) THEN
  577.             HandleDocumentEvent(theWind, theEvent);
  578.             
  579.         SetMenuBarState;
  580.     END;
  581.  
  582. {$S EventMgmt}
  583. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  584. {                                                                                      }
  585. {    DoApp4Evt                                                                          }
  586. {                                                                                      }
  587. {    The application is either being switched in or out of under multifinder.   The      }
  588. {    approprate actions need to be performed to handle window and control appearence.  }
  589. {                                                                                      }
  590. {    October 13, 1991    WHK        Created today                                          }
  591. {                                                                                      }
  592. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  593. PROCEDURE DoApp4Evt(theEvent    : EventRecord);
  594.     BEGIN
  595.     END;
  596.  
  597. {$S EventMgmt}
  598. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  599. {                                                                                      }
  600. {    HandleEvent                                                                            }
  601. {                                                                                      }
  602. {    An event has occured and now needs to be handled in the approprate area. This is  }
  603. {    where currently unsupported Apple Events will need to be added.                      }
  604. {                                                                                      }
  605. {    October 13, 1991    WHK        Created today                                          }
  606. {                                                                                      }
  607. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  608. PROCEDURE HandleEvent(theEvent    : EventRecord);
  609.     BEGIN
  610.         CASE theEvent.what OF
  611.             mouseDown:        DoMouseDown(theEvent);
  612.             keyDown:        DoKeyDown(theEvent);
  613.             AutoKey:        DoKeyDown(theEvent);
  614.             UpdateEvt:        DoUpdate(theEvent);
  615.             ActivateEvt:    DoActivate(theEvent);
  616.             App4Evt:        DoApp4Evt(theEvent);
  617.             OTHERWISE
  618.                 ;
  619.         END;
  620.     END;
  621.     
  622. {$S EventMgmt}
  623. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  624. {                                                                                      }
  625. {    DoMainEventLoop                                                                      }
  626. {                                                                                      }
  627. {    No longer a loop but a simple check to see if an event has occured.  If it has,      }
  628. {    then the event is passed on to the HandleEvent routine.  The advantage of having  }
  629. {    this check outside the repeat-until loop allows for event handling during          }
  630. {    program processing.                                                                  }
  631. {                                                                                      }
  632. {    October 13, 1991    WHK        Created today                                          }
  633. {                                                                                      }
  634. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  635. PROCEDURE DoMainEventLoop;
  636.     VAR
  637.         tempStr        : Str255;
  638.         theEvent    : EventRecord;
  639.         error        : OSErr;
  640.     BEGIN
  641.         error := YieldToAnyThread;
  642.         IF error <> noErr THEN DebugStr('Trouble yielding in main event loop');
  643.  
  644.         IF WaitNextEvent(everyEvent,theEvent, 1, NIL) THEN
  645.             HandleEvent(theEvent);
  646.     END;
  647.  
  648. {$S EventMgmt}
  649. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  650. {                                                                                      }
  651. {    ThreadsIsAvailable                                                                  }
  652. {                                                                                      }
  653. {    Uses gestalt to verify the availability of the Thread Manager.  Assumption that      }
  654. {    gestalt is present.                                                                  }
  655. {                                                                                      }
  656. {    July 31, 1991        WHK        Created today                                          }
  657. {                                                                                      }
  658. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  659. FUNCTION ThreadsIsAvailable    : BOOLEAN;
  660.     VAR
  661.         error        : OSErr;
  662.         response    : LONGINT;
  663.     BEGIN
  664.         ThreadsIsAvailable := FALSE;
  665.         error := Gestalt(gestaltThreadMgrAttr, response);
  666.         IF error = noErr THEN
  667.             IF BTST(response, gestaltThreadMgrPresent) THEN
  668.                 ThreadsIsAvailable := TRUE;
  669.     END;
  670.     
  671. {$S EventMgmt}
  672. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  673. {                                                                                      }
  674. {    RunApplication                                                                      }
  675. {                                                                                      }
  676. {    Simply run throught the main event loop until gDone is TRUE.                      }
  677. {                                                                                      }
  678. {    October 13, 1991    WHK        Created today                                          }
  679. {    July 31, 1991        WHK        Added check to see if Threads Manager is available.      }
  680. {                                                                                      }
  681. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  682. PROCEDURE RunApplication;
  683.     VAR
  684.         itemHit        : INTEGER;
  685.     BEGIN
  686.         IF ThreadsIsAvailable THEN
  687.             BEGIN
  688.                 CreateNewDocument;
  689.                 repeat
  690.                     DoMainEventLoop;
  691.                 until gDone;
  692.             END
  693.         ELSE
  694.             BEGIN
  695.                 itemHit := StopAlert(rNoThreadMgrAlertID, NIL);
  696.             END;
  697.     END;
  698.     
  699. {$S EventMgmt}
  700. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  701. {                                                                                      }
  702. {    CleanUpOnExit                                                                      }
  703. {                                                                                      }
  704. {    The program has quit and this is the last routine that will be called before the  }
  705. {    application quits.  Any last minute cleaning up and preference saving will need   }
  706. {    to be done here.                                                                  }
  707. {                                                                                      }
  708. {    October 13, 1991        Created by William Knott                                  }
  709. {                                                                                      }
  710. {––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––-––}
  711. PROCEDURE CleanUpOnExit;
  712.     BEGIN
  713.         { Nothing to clean up right now.  In all honesty, probably should dispose of all the threads here.    }
  714.     END;
  715.  
  716. END.